package com.copera.cn.kaka;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.copera.cn.kaka.util.ActionUrls;
import com.copera.cn.kaka.util.ActivityManager;
import com.copera.cn.kaka.util.HttpUtils;
import com.copera.cn.kaka.vo.ShopVo;
/**
 * 
 * @Description: 记录足迹
 *
 * @author lyc
 *
 * @date 2014年7月9日
 */
public class TrackRecordActivity extends BaseActivity{
	
	private Button goBack,affirm,cancel;
	private LocationClient mLocClient;
	public MyLocationListenner myListener = new MyLocationListenner();
	private String tude = "";//经纬度
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ActivityManager.getInstance().push(this);
		setContentView(R.layout.activity_trackrecord);
		
		goBack = (Button)findViewById(R.id.bt_img);
		affirm = (Button)findViewById(R.id.bt_02);
		cancel = (Button)findViewById(R.id.bt_03);
		initEvent();
		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {

				case ActionUrls.FAILURE:
					Toast.makeText(TrackRecordActivity.this, (String) msg.obj,
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
		};
		mLocClient = new LocationClient(this);
		mLocClient.registerLocationListener(myListener);
		setLocationOption();
		mLocClient.start();
		//强制为竖屏 
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	}
	
	private void initEvent(){
		goBack.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				ShopVo shop = getAppCustomer().getShop();
				shop.setIfTime("true");
				finish();
			}
		});
		cancel.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				ShopVo shop = getAppCustomer().getShop();
				shop.setIfTime("true");
				finish();
			}
		});
		affirm.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				new Thread() {
					public void run() {
						try {
							ShopVo shop = getAppCustomer().getShop();
							Intent intent = getIntent();
							String str = "";
							if ("1".equals(intent.getStringExtra("state"))) {
								str = shop.getTrackId();
							}
							String json = HttpUtils.get(intent.getStringExtra("url")+tude+"&trackId="+str, getAppCustomer().getHttpClient());
							if(json!=null){
								JSONObject jo = new JSONObject(json);
								String message = jo.getString("message");
								if(message.equals("保存成功")){
									shop.setTrackToday(jo.getString("trackToday"));
									shop.setTrackId(jo.getString("trackId"));
									shop.setIfTime("false");
									finish();
								}else{
									Message msg = handler.obtainMessage(ActionUrls.FAILURE, message);
									handler.sendMessage(msg);
								}
							}
						} catch (ClientProtocolException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						} catch (JSONException e) {
							e.printStackTrace();
						}
					};
				}.start();
			}
		});
	}
	@Override
	public void onDestroy() {
		mLocClient.stop();
		tude = "";
		super.onDestroy();
	}

	//设置相关参数
	private void setLocationOption(){
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true);//打开gps
		option.setServiceName("com.baidu.location.service_v2.9");
		
		option.setScanSpan(2000);
		
		//显示详细信息
//		option.setPoiExtraInfo(true);	
//		option.setAddrType("all");
		
		option.setPoiNumber(10);
		option.disableCache(true);		
		mLocClient.setLocOption(option);
	}
	/**
	 * 监听函数，又新位置的时候，格式化成字符串
	 */
	public class MyLocationListenner implements BDLocationListener {
		@Override
		public void onReceiveLocation(BDLocation location) {
			if (location == null)
				return ;
			StringBuffer sb = new StringBuffer(256);
			sb.append("&latitude=");
			sb.append(location.getLatitude());
			sb.append("&longitude=");
			sb.append(location.getLongitude());
//			if (location.getLocType() == BDLocation.TypeGpsLocation){
//				Toast.makeText(TrackRecordActivity.this, "speed:"+location.getSpeed()+" satellite:"+location.getSatelliteNumber(),Toast.LENGTH_SHORT).show();
//			}else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
//				Toast.makeText(TrackRecordActivity.this, location.getAddrStr(),Toast.LENGTH_SHORT).show();
//			}
			tude = sb.toString();
		}
		
		public void onReceivePoi(BDLocation poiLocation) {
			if (poiLocation == null){
				return ; 
			}
		}
	}
}
